﻿using System;

namespace CoreRay
{
    /// <summary>
    /// Represents a vertex format element.
    /// </summary>
    public class VertexFormatElement : IEquatable<VertexFormatElement>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="VertexFormatElement"/> class.
        /// </summary>
        /// <param name="name">Name of the element.</param>
        /// <param name="type">Type of the element.</param>
        internal VertexFormatElement(string name, Type type)
        {
            // Init private members
            this.Name = name;
            this.Type = type;

            // Compute element size
            this.Size = ComputeElementSize(type);
        }

        /// <summary>
        /// Gets the name of the element.
        /// </summary>
        /// <value>Name of the element.</value>
        public string Name
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the type of the element.
        /// </summary>
        /// <value>Type of the element.</value>
        public Type Type
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the size of the element in bytes.
        /// </summary>
        /// <value>Size of the element in bytes.</value>
        public int Size
        {
            get;
            private set;
        }

        /// <summary>
        /// Computes the size of the element.
        /// </summary>
        /// <param name="type">Type of the element.</param>
        /// <returns>Size of the element in bytes.</returns>
        private int ComputeElementSize(Type type)
        {
            if(type == typeof(float))
            {
                return 4;
            }

            else if (type == typeof(Vector2))
            {
                return 8;
            }

            else if (type == typeof(Vector3))
            {
                return 12;
            }

            return 0;
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public bool Equals(VertexFormatElement other)
        {
            return this.Name == other.Name && this.Size == other.Size && this.Type == other.Type;
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            VertexFormatElement element = obj as VertexFormatElement;

            if(element != null)
            {
                return this.Equals(element);
            }

            return false;
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="element1">The element1.</param>
        /// <param name="element2">The element2.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(VertexFormatElement element1, VertexFormatElement element2)
        {
            return element1.Equals(element2);
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="element1">The element1.</param>
        /// <param name="element2">The element2.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(VertexFormatElement element1, VertexFormatElement element2)
        {
            return !element1.Equals(element2);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return this.Name.GetHashCode() | this.Size.GetHashCode() | this.Type.GetHashCode();
        }
    }
}
